package com.jiafa.wjxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jiafa.wjxt.vo.ExportProjectResultVO;
import com.jiafa.wjxt.entity.TUserProjectItem;
import com.jiafa.wjxt.entity.TUserProjectResult;
import com.jiafa.wjxt.mapper.TUserProjectResultMapper;
import com.jiafa.wjxt.service.TUserProjectItemService;
import com.jiafa.wjxt.service.TUserProjectResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiafa.wjxt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiafa
 * @since 2021-05-31
 */
@Service
public class TUserProjectResultServiceImpl extends ServiceImpl<TUserProjectResultMapper, TUserProjectResult> implements TUserProjectResultService {
    @Autowired
    TUserProjectResultMapper tUserProjectResultMapper;

    @Autowired
    TUserProjectItemService tUserProjectItemService;
    @Override
    public Result queryAllResult(String projectKey) {
        Result result = Result.build();
        QueryWrapper<TUserProjectResult> wrapper = new QueryWrapper<>();
        System.out.println(projectKey);
        if(projectKey != null){
            wrapper.eq("project_key",projectKey);
        }
        List<TUserProjectResult> tUserProjectResults = tUserProjectResultMapper.selectList(wrapper);
        System.out.println(tUserProjectResults);
        result.setCode(200);
        result.setData(tUserProjectResults);
        return result;
    }

    @Override
    public ExportProjectResultVO exportResult(String projectKey) {
        System.out.println("11projectKey: " + projectKey);
        //问题列表
        List<TUserProjectItem> userProjectItemEntityList = tUserProjectItemService.listByProjectKey(projectKey);
        System.out.println("userProjectItemEntityList: " + userProjectItemEntityList);
        // excel 标题列
        List<ExportProjectResultVO.ExcelHeader> titleList = userProjectItemEntityList.stream()
                .map(item -> new ExportProjectResultVO.ExcelHeader(item.getFormItemId(), item.getLabel()))
                .collect(Collectors.toList());
        //结果
        List<TUserProjectResult> resultEntityList = this.list(Wrappers.<TUserProjectResult>lambdaQuery()
                .eq(TUserProjectResult::getProjectKey, projectKey));
        List<Map<String, Object>> resultList = resultEntityList.stream().map(item -> {
            Map<String, Object> processData = item.getProcessData();
            Iterator<String> iterator = processData.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (!titleList.stream()
                        .map(ExportProjectResultVO.ExcelHeader::getFieldKey).collect(Collectors.toList()).contains(key)) {
                    iterator.remove();
                }
            }

            processData.put(com.jiafa.wjxt.entity.BaseEntity.Fields.createTime, item.getCreateTime());
            return processData;
        }).collect(Collectors.toList());
        List<ExportProjectResultVO.ExcelHeader> allHeaderList = ExportProjectResultVO.DEFAULT_HEADER_NAME;
        allHeaderList.addAll(titleList);
        return new ExportProjectResultVO(allHeaderList, resultList);
    }
}
